(function($) {

	$.extend(mejs.MepDefaults, {
		newWindowCallback: function() { return '';},
		fullscreenText: 'Fullscreen'
	});

	$.extend(MediaElementPlayer.prototype, {

		isFullScreen: false,

		isNativeFullScreen: false,

		isInIframe: false,

		buildfullscreen: function(player, controls, layers, media) {

			player.isInIframe = (window.location != window.parent.location);

			// Native
			if (mejs.MediaFeatures.hasTrueNativeFullScreen) {

				// Chrome doesn't always fire this in an iframe
				var func = function(e) {
					if (player.isFullScreen) {
						if (mejs.MediaFeatures.isFullScreen()) {
							player.isNativeFullScreen = true;
							// Reset the controls once we are fully in full screen
							player.setControlsSize();
						} else {
							player.isNativeFullScreen = false;
							// When a user presses ESC make sure to put the player back into place
							player.exitFullScreen();
						}
					}
				};

				if (mejs.MediaFeatures.hasMozNativeFullScreen) {
					player.globalBind(mejs.MediaFeatures.fullScreenEventName, func);
				} else {
					player.container.bind(mejs.MediaFeatures.fullScreenEventName, func);
				}
			}

			var t = this,
				normalHeight = 0,
				normalWidth = 0,
				container = player.container,
				fullscreenBtn =
					$('<div class="mejs-button mejs-fullscreen-button">' +
						'<button type="button" aria-controls="' + t.id + '" title="' + t.options.fullscreenText + '" aria-label="' + t.options.fullscreenText + '"></button>' +
					'</div>')
					.appendTo(controls);


                                fullscreenBtn.click(function() {
                                        var isFullScreen = (mejs.MediaFeatures.hasTrueNativeFullScreen && mejs.MediaFeatures.isFullScreen()) || player.isFullScreen;

                                        if (isFullScreen) {
                                                player.exitFullScreen();
                                        } else {
                                                player.enterFullScreen();
                                        }
                                });

			player.fullscreenBtn = fullscreenBtn;

			t.globalBind('keydown',function (e) {
				if (((mejs.MediaFeatures.hasTrueNativeFullScreen && mejs.MediaFeatures.isFullScreen()) || t.isFullScreen) && e.keyCode == 27) {
					player.exitFullScreen();
				}
			});

		},

		cleanfullscreen: function(player) {
			player.exitFullScreen();
		},

        containerSizeTimeout: null,

		enterFullScreen: function() {

			var t = this;
                        
			// Set it to not show scroll bars so 100% will work
                        $(document.documentElement).addClass('mejs-fullscreen');
                        
			// Store sizing
			normalHeight = t.container.height();
			normalWidth = t.container.width();

			// Attempt to do true fullscreen
                        if (mejs.MediaFeatures.hasTrueNativeFullScreen) {

                                mejs.MediaFeatures.requestFullScreen(t.container[0]);

                                if (t.isInIframe) {
                                        // Sometimes exiting from fullscreen doesn't work
                                        setTimeout(function checkFullscreen() {

                                                if (t.isNativeFullScreen) {

                                                        // Check if the video is suddenly not really fullscreen
                                                        if ($(window).width() !== screen.width) {
                                                                // manually exit
                                                                t.exitFullScreen();
                                                        } else {
                                                                // test again
                                                                setTimeout(checkFullscreen, 500);
                                                        }
                                                }


                                        }, 500);
                                }

                        } else if (mejs.MediaFeatures.hasSemiNativeFullScreen) {
                                t.media.webkitEnterFullscreen();
                                return;
                        }
                                
			// Check for iframe launch
			if (t.isInIframe) {
				var url = t.options.newWindowCallback(this);


				if (url !== '') {

					// Launch immediately
					if (!mejs.MediaFeatures.hasTrueNativeFullScreen) {
						t.pause();
						window.open(url, t.id, 'top=0,left=0,width=' + screen.availWidth + ',height=' + screen.availHeight + ',resizable=yes,scrollbars=no,status=no,toolbar=no');
						return;
					} else {
						setTimeout(function() {
							if (!t.isNativeFullScreen) {
								t.pause();
								window.open(url, t.id, 'top=0,left=0,width=' + screen.availWidth + ',height=' + screen.availHeight + ',resizable=yes,scrollbars=no,status=no,toolbar=no');
							}
						}, 250);
					}
				}

			}

			// Full window code

			// Make full size
			t.container
				.addClass('mejs-container-fullscreen')
				.width('100%')
				.height('100%');

			// Only needed for safari 5.1 native full screen, can cause display issues elsewhere
                        t.containerSizeTimeout = setTimeout(function() {
                                t.container.css({width: '100%', height: '100%'});
                                t.setControlsSize();
                        }, 500);

                        t.$media
                                .width('100%')
                                .height('100%');

			t.layers.children('div')
				.width('100%')
				.height('100%');

			if (t.fullscreenBtn) {
				t.fullscreenBtn
					.removeClass('mejs-fullscreen')
					.addClass('mejs-unfullscreen');
			}

			t.setControlsSize();
			t.isFullScreen = true;
                        
                        t.container.find('.mejs-captions-text').css('font-size', screen.width / t.width * 1.00 * 100 + '%');
                        t.container.find('.mejs-captions-position').css('bottom', '45px');
		},

		exitFullScreen: function() {

			var t = this;
                        
                        // Prevent container from attempting to stretch a second time
                        clearTimeout(t.containerSizeTimeout);

			// Come out of native fullscreen
			if (mejs.MediaFeatures.hasTrueNativeFullScreen && (mejs.MediaFeatures.isFullScreen() || t.isFullScreen)) {
				mejs.MediaFeatures.cancelFullScreen();
			}

			// Restore scroll bars to document
                        $(document.documentElement).removeClass('mejs-fullscreen');

			t.container
				.removeClass('mejs-container-fullscreen')
				.width(normalWidth)
				.height(normalHeight);

                        t.$media
                                .width(normalWidth)
                                .height(normalHeight);

			t.layers.children('div')
				.width(normalWidth)
				.height(normalHeight);

			t.fullscreenBtn
				.removeClass('mejs-unfullscreen')
				.addClass('mejs-fullscreen');

			t.setControlsSize();
			t.isFullScreen = false;
                        
                        t.container.find('.mejs-captions-text').css('font-size','');
                        t.container.find('.mejs-captions-position').css('bottom', '');
		}  
	});

})(mejs.$);
